<template>
  <div class="coursedetail">
    <div class="center-content">
      <!-- 当前位置 -->
      <header>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">课程</el-breadcrumb-item>
          <el-breadcrumb-item>课程详情</el-breadcrumb-item>
          <el-breadcrumb-item>{{ course && course.title }}</el-breadcrumb-item>
        </el-breadcrumb>
      </header>
      <!-- 课程页面 -->
      <div class="play-page clearfix" v-if="course">
        <div class="play-page-left">
          <img v-if="!playStatus" :src="course.image" :alt="course.title" />
          <video v-else controls="controls" :src="playUrl" autoplay></video>
        </div>
        <div class="play-page-info">
          <p title="Spring&nbsp;Boot实战入门—黑马分布式网盘系统开发" class="title ellipsis">
            {{ course.title }}
          </p>
          <ul class="courseinfo clearfix">
            <li>
              章节数
              <p>{{ course.courseChapterList.length }}章</p>
            </li>
            <li>
              课时
              <p>{{ formatTime(course.learningTime) }}</p>
            </li>
            <li>
              学习人数
              <p>{{ 332 }}人</p>
            </li>
          </ul>
          <div style="text-align: center; margin-top: 50px">
            <el-button v-if="course.signUpStatus === null" plain @click="signUpCourse()">报名课程</el-button>
            <el-button v-else disabled plain >已报名</el-button>
          </div>
        </div>
      </div>
      <!-- 课程详情 -->
      <div class="play-detail clearfix" v-if="course">
        <div class="play-detail-left">
          <el-tabs v-model="tabActive">
            <el-tab-pane label="课程介绍" name="课程介绍">
              <div class="playdetail-ctx">
                <div v-html="course.info"></div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="课程学习" name="课程学习">
              <div class="catalog-ctx">
                <el-collapse :model="chapterActive" accordion>
                  <el-collapse-item v-for="item in course.courseChapterList"
                      :key="item.id" :title="item.title" :name="item.id + ''">
                    <div class="catalog-item active" v-for="item2 in item.courseChapterVideoList" :key="item2.id" @click="changeVideo(item2)">
                      <el-row>
                        <el-col :span="8" style="text-align: left"><span>{{ item2.title }}</span></el-col>
                        <el-col :span="8"><span>时长：{{ formatTime(item2.videoTime) }}</span></el-col>
                        <el-col :span="8" style="text-align: right"><span><el-tag type="success" v-if="item2.studyStatus === 1">已完成</el-tag></span></el-col>
                      </el-row>
                    </div>
                  </el-collapse-item>
                </el-collapse>
              </div>
            </el-tab-pane>
            <el-tab-pane label="考试测验" name="考试测验">
              <div v-if="course.courseExam" class="card-exam-box">
                <div class="clearfix">
                  <div style="float: left">
                    <div class="ellipsis">{{ course.title }}</div>
                    <div class="font-desc">题目数：{{ course.courseExam.questionCount }}</div>
                  </div>
                  <div style="float: right">
                    <div class="font-desc">总分：{{ course.courseExam.fullScore }}</div>
                    <div class="font-desc">时长：{{ formatTime(course.courseExam.durationTime) }}</div>
                  </div>
                </div>
                <div style="margin-top: 10px; text-align: right">
                  <el-button @click="getExamPaperList(course.courseExam.id)" size="mini">开始答题</el-button>
                </div>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
        <!-- 右侧讲师介绍 -->
        <div class="play-detail-right">
          <div class="right-mok">
            <p class="title">讲师介绍</p>
            <div class="info-box" v-for="item in course.courseLecturerList" :key="item.id">
              <div class="info">
                <img style="width: 50px" src="https://scykt-file.oss-cn-shanghai.aliyuncs.com/default.png" alt="" />
                <div class="info-author">
                  <span>{{ item.name }}</span>
                  <p>{{ item.job }}</p>
                </div>
              </div>
              <div class="text">{{ item.info }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="试卷列表" v-model="scoreDialogVisible" width="60%">
      <div class="answer-body" v-for="item in examPaperList" :key="item.id">
        <div class="answer-item">
          <div class="font-desc">更新时间：{{ item.updateTime }}</div>
          <div class="font-desc">提交状态：
            <el-tag v-if="item.submitStatus === 0" type="warning">待提交</el-tag>
            <el-tag v-if="item.submitStatus === 1" type="success">已提交</el-tag>
          </div>
        </div>
        <div class="answer-item">
          <div class="font-desc">题目数量：{{ course.courseExam.questionCount }}</div>
          <div class="font-desc">试卷总分：{{ course.courseExam.fullScore }}</div>
        </div>
        <div class="answer-item">
          <div class="font-desc">通过状态：
            <el-tag v-if="item.paperSuccess === 0" type="warning">未通过</el-tag>
            <el-tag v-if="item.paperSuccess === 1" type="success">已通过</el-tag>
          </div>
          <div class="font-desc">最终得分：{{ item.paperScore }}</div>
        </div>
      </div>
      <span class="dialog-footer">
        <el-button @click="scoreDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="startExam(examPaperList,course.courseExam.id)">继续考试</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getCourseDetail,signUpCourse } from "@/api/u/course.js";
import { getExamPaperList,startExam } from "@/api/u/exam.js";
import { formatTime } from "@/util/dateUtils.js";
export default {
  data() {
    return {
      tabActive: "课程介绍",
      course: null,
      chapterActive: "1",
      playStatus: false,
      playUrl: "",
      scoreDialogVisible: false,
      examPaperList: []
    };
  },
  created() {
    if (this.$route.query && this.$route.query.id) {
      this.getCourseDetail(this.$route.query.id);
    }
  },
  computed: {
    ...mapState({ userInfo: (state) => state.user }),
  },
  methods: {
    // 获取课程详情页数据
    getCourseDetail(id) {
      let param = {
        courseId: id
      }
      getCourseDetail(param).then((res) => {
        if (res.code === 200) {
          this.course = res.data;
        }
      });
    },
    // 格式化时间
    formatTime(data){
      return formatTime(data);
    },
    // 更换播放视频
    changeVideo(item){
      console.log("播放视频")
      this.playStatus = true;
      this.playUrl = item.url;
    },
    signUpCourse(){
      this.$confirm("是否报名该课程？")
          .then(() => {
            let param = {
              courseId: this.course.id
            }
            signUpCourse(param).then((res) => {
              if (res.code === 200) {
                this.$message.success("报名课程成功");
                location.reload();
              } else {
                this.$message.success(res.message);
                location.reload();
              }
            });
          })
          .catch(() => {});
    },
    // 获取已参加考试的试卷列表
    getExamPaperList(id){
      let param = {
        examId: id
      }
      getExamPaperList(param).then((res) => {
        if (res.code === 200) {
          this.examPaperList = res.data;
          this.scoreDialogVisible = true;
        } else {
          this.$message.success(res.message);
          location.reload();
        }
      });
    },
    // 开始考试
    startExam(examPaperList,id) {
      let submitStatus = true;
      let paperId = 0;
      examPaperList.forEach(item=> {
        if (item.submitStatus===0) {
          submitStatus = false;
          paperId = item.id;
        }
      })
      if (submitStatus) {
        let param = {
          examId: id
        }
        startExam(param).then((res) => {
          if (res.code === 200) {
            console.log("试卷生成成功");
            this.$router.push(`/examAnswerDetail?id=${res.data.id}`);
          } else {
            this.$message.success(res.message);
            location.reload();
          }
        });
      } else {
        this.$router.push(`/examAnswerDetail?id=${paperId}`);
      }
    }
  },
};
</script>
<style lang="scss">
.coursedetail .el-collapse-item__content {
  padding-bottom: 0;
  padding-left: 15px;
}
.coursedetail .el-collapse-item__header {
  background-color: #fcfcfc;
  padding: 0 10px;
}
.coursedetail {
  width: 100%;
  background-color: #f7f7f7;
  overflow: hidden;
  padding-bottom: 80px;
  .center-content {
    height: 100%;
    min-height: 800px;
    padding-top: 20px;
  }
  .card-exam-box {
    text-align: left;
    font-size: 14px;
    line-height: 30px;
    border-bottom: 1px solid #f7f7f7;
    padding: 10px;
  }
  .answer-body {
    padding: 5px;
    display: flex;
    flex-direction: row;
    .answer-item {
      text-align: left;
      flex: 1;
      background-color: #fff;
      display: flex;
      flex-direction: column;
      padding: 10px 20px 10px 20px;
      line-height: 40px;
    }
  }
}
header {
  margin-bottom: 20px;
}
.play-page {
  .play-page-left {
    position: relative;
    float: left;
    width: 910px;
    height: 570px;
    background-color: #000;
    overflow: hidden;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    & > img {
      width: 100%;
      height: 100%;
    }
    & > video {
      width: 100%;
      height: 100%;
    }
  }
  .play-page-info {
    float: right;
    width: 290px;
    height: 570px;
    background-color: #1b1d25;
    color: #fff;
    padding: 18px 10px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    .title {
      font-size: 16px;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    .courseContent {
      padding: 18px 0;
      font-size: 12px;
      border-bottom: 1px solid #333;
      word-break: break-all;
      word-wrap: break-word;
      p {
        color: #ccc;
      }
      .courseContent-txt {
        font-size: 12px;
        color: #999;
        line-height: 22px;
        display: inline-block;
        margin-top: 3px;
        text-indent: 2px;
        word-break: break-all;
        word-wrap: break-word;
      }
    }
    .courseinfo {
      border-bottom: 1px solid #333;
      padding: 16px 0;

      li {
        float: left;
        width: 70px;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        text-align: center;
        &:nth-child(2) {
          text-align: center;
          width: 100px;
          margin: 0 10px;
        }
        p {
          font-size: 14px;
          color: #999;
          margin-top: 12px;
          text-align: center;
        }
      }
    }
    .author {
      margin-top: 20px;
      border-bottom: 1px solid #333;
      padding-bottom: 18px;
    }
  }
}

.play-detail {
  margin-top: 20px;
  box-sizing: content-box;
  .play-detail-left {
    float: left;
    padding: 13px;
    box-sizing: border-box;
    width: 890px;
    background-color: #fff;
    .playdetail-ctx {
      padding: 20px 30px;
      min-height: 686px;
      line-height: 22px;
      word-break: break-all;
      word-wrap: break-word;
      img {
        width: 100%;
      }
    }
    .catalog-ctx {
      padding: 20px;
      .catalog-item {
        cursor: pointer;
        padding: 10px;
        border-bottom: 1px solid #f8f8f8;
        i {
          margin-left: 15px;
          font-size: 12px;
          vertical-align: middle;
          margin-bottom: 2px;
        }
        &:hover {
          background: #f0f6ff;
        }
        &.finish {
          i {
            color: #52cc5c;
          }
        }
        &.active {
          i {
            color: $theme-color-font;
          }
        }
      }
    }
  }
  .play-detail-right {
    float: right;
    width: 290px;
    .right-mok {
      background-color: #fff;
      .title {
        padding: 17px 20px;
        font-size: 16px;
        border-bottom: 1px solid #e5e5e5;
      }
      .info-box {
        cursor: pointer;
        padding: 15px 20px;
        font-size: 14px;
        color: #333;
        .text {
          text-indent: 28px;
          line-height: 24px;
          margin: 15px 0;
        }
        .info {
          display: flex;
          align-items: center;
          .info-author {
            margin-left: 10px;
            line-height: 25px;
            span {
              font-weight: bold;
            }
            p {
              font-size: 12px;
            }
          }
        }
      }
    }
  }
}
</style>
